package com.mrent.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Order;
import com.mrent.entity.OrderManager;
import com.mrent.service.OrderManagerService;
import com.mrent.service.OrderService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;

@Controller
public class AdminOrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderManagerService orderManagerService;
	
	/**
	 * 申请管理.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/order/index")
	public String index(){
		
		return "admin/order/home";
	}
	/**
	 * 申请列表.
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/order/list")
	public String list(String page,HttpServletResponse response){
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		
		List<Order> list = orderService.listOrder(pp);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("rows", list);
		json.put("total", pp.getTotalCount());
		RenderUtil.renderJson(json,response);
		
		return null;
	}
	
	/**
	 * 删除申请.
	 * 
	 * @param orderId
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/order/del/{orderId}")
	public String del(@PathVariable Long orderId,HttpServletResponse response){
		
		orderService.delOrder(orderId);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		
		return null;
	}
	
	/**
	 * 修改订单.
	 * 
	 * @param memberId
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/order/modify/{orderId}")
	public String modify(@PathVariable Long orderId,Integer state,HttpServletResponse response){
		Order order = orderService.getOrder(orderId);
		if(order != null){
			order.setOrderState(state);
			orderService.updateOrder(order);
			//管理员相关.
			OrderManager orderManager = orderManagerService.getByOrderNo(order.getOrderNo());
			if(orderManager == null){
				orderManager = new OrderManager();
				orderManager.setOrderNo(order.getOrderNo());
				orderManager.setOrderMember(order.getOrderMember());
				orderManager.setOrderRequestYear(order.getOrderRequestYear());
				orderManager.setOrderRequestMonth(order.getOrderRequestMonth());
				orderManager.setOrderRequestDay(order.getOrderRequestDay());
				orderManager.setOrderEndYear(order.getOrderEndYear());
				orderManager.setOrderEndMonth(order.getOrderEndMonth());
				orderManager.setOrderEndDay(order.getOrderEndDay());
				orderManager.setState(state);
				orderManager.setLoginPwd(RandomStringUtils.randomAlphanumeric(10));
				orderManagerService.saveOrderManager(orderManager);
				
				//发送邮件.
			}else {
				orderManager.setState(state);
				orderManagerService.modifyOrderManager(orderManager);
			}
		}
		
		
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}

}
